import $ from "jquery";
import {url} from "../../util/config";
import store from "../../store/store";
import {error, success} from "../../util/responseStatus";
import {message} from "antd";
import login from "../login";

let config = require('../../util/config').config

function getNewLogin(values){
    return updateFile(values)
}
export default function updateFile(values) {
    let token = localStorage.getItem("token")
    if(values.key!=undefined && values.type!=undefined){
        if(values.type==config.file){
            if(values.new==undefined && values.name==undefined && values.status==undefined&&values.attachUrls==undefined&&values.attachNames==undefined){
                alert(config.file+"无修改内容")
                return
            }
        }else if(values.type==config.article){
            if(values.new==undefined && values.name==undefined && values.status==undefined&&values.attachUrls==undefined&&values.attachNames==undefined&&values.attachNum==undefined&&values.content==undefined){
                alert(config.article+"无修改内容")
                return
            }
        }else {
            if(values.new==undefined && values.name==undefined && values.status==undefined){
                alert(config.folder+"无修改内容")
                return
            }
        }
    }

    let newDate;
    //articleId/fileId或者folderId
    let key = values.key
    //config.file/config.article或者config.folder
    let type = values.type;

    if(type==config.file){
        type = "file"
        newDate = {
            //fileId
            NOTIFY_DATE:values.new,
            ID:key,
            FILE_TYPE:type,
            FILE_NAME:values.name,
            TOP_STATUS:values.status,
            FILE_URL:values.attachUrls,
            FILE_URL_NAME: values.attachNames
        }

    } else if(type==config.article){
        type = "article"
        newDate = {
            //articleId
            ARTICLE_NEW:values.new,
            ID:key,
            FILE_TYPE:type,
            ARTICLE_NAME:values.name,
            TOP_STATUS:values.status,
            ARTICLE_CONTENT:values.content,
            ATTACH_URL:values.attachUrls,
            ATTACH_NAME:values.attachNames,
            ATTACH_NUM:values.attachNum
        }
    }else if(type==config.folder){
        type = "folder"
        newDate = {
            //folderId
            NOTIFY_DATE:values.new,
            ID:key,
            FILE_TYPE:type,
            FOLDER_NAME:values.name,
            TOP_STATUS:values.status,
        }
    }

    let status = null

    $.ajax({
        async:false,
        headers: {
            'Accept': "application/json",
            'Authorization': token
        },
        url: url.api_url+"FileList/UpdateFile",
        type: 'POST',
        contentType:"application/json",
        data: JSON.stringify(newDate),
        success: function (result) {
            if(result.result=="Token過期請重新登錄"){
                login(localStorage.getItem("account"), localStorage.getItem("password"));
                status = getNewLogin(values);
            }else {
                if(result.result=="ok"){
                    message.success("修改成功")
                    status = "ok"
                }else {
                    message.error("修改失败")
                    status = "error"
                }
            }

        },
        error:function (error) {
            message.error("请求异常")
            status = "error"
        }
    })

    return status
}
